package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.StaffWalletItem;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.StaffWalletAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 员工钱包
 */
public class StaffWalletActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.tv_bill_title)
    TextView tvBillTitle;
    @BindView(R.id.iv_bill_time_left)
    ImageView ivBillTimeLeft;
    @BindView(R.id.tv_bill_money)
    TextView tvBillMoney;
    @BindView(R.id.iv_bill_time_right)
    ImageView ivBillTimeRight;
    @BindView(R.id.tv_bill_time_left)
    TextView tvBillTimeLeft;
    @BindView(R.id.tv_bill_time_right)
    TextView tvBillTimeRight;
    @BindView(R.id.rv_service_bill)
    RecyclerView rvServiceBill;

    private List<StaffWalletItem> data;
    private StaffWalletAdapter staffWalletAdapter;
    private String month;
    private int index;

    public static Intent createIntent(Context context){
        return new Intent(context,StaffWalletActivity.class);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_staff_wallet);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        txtTitle.setText("员工钱包");
        ivBillTimeLeft.setClickable(false);
        ivBillTimeLeft.setSelected(false);
        ivBillTimeRight.setClickable(false);
        ivBillTimeRight.setSelected(false);

        rvServiceBill.setLayoutManager(new LinearLayoutManager(this));
        List<StaffWalletItem.ServiceFlowItem> list = new ArrayList<>();
        staffWalletAdapter = new StaffWalletAdapter(StaffWalletActivity.this, R.layout.item_staff_wallet, list);
        rvServiceBill.setAdapter(staffWalletAdapter);
        RetrofitSource.createApi(HttpPostService.class)
                .tec_monye(SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<StaffWalletItem>>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<List<StaffWalletItem>> response) {
                        data = response.getData();
                        index = 0;
                        month = data.get(index).getMonth();
                        if (data.size() > 1) {
                            ivBillTimeLeft.setClickable(true);
                            ivBillTimeLeft.setSelected(true);
                            tvBillTimeLeft.setText(String.format(ResourcesUtils.getString(R.string.service_flow_month), data.get(index + 1).getMonth()));
                        }
                        setMoneyAndList();
                    }
                });
    }

    private void setMoneyAndList() {
        tvBillTitle.setText(String.format(ResourcesUtils.getString(R.string.service_flow_title), month));
        tvBillMoney.setText(String.format(ResourcesUtils.getString(R.string.money), data.get(index).getMoney()));
        staffWalletAdapter.setDatas(data.get(index).getList());
    }

    @OnClick({R.id.ll_leftview, R.id.iv_bill_time_left, R.id.iv_bill_time_right})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.iv_bill_time_left:
                index++;
                month = data.get(index).getMonth();
                setMoneyAndList();
                if (index == data.size() - 1) {
                    ivBillTimeLeft.setClickable(false);
                    ivBillTimeLeft.setSelected(false);
                    tvBillTimeLeft.setText("");
                } else {
                    tvBillTimeLeft.setText(String.format(ResourcesUtils.getString(R.string.service_flow_month), data.get(index + 1).getMonth()));
                }
                tvBillTimeRight.setText(String.format(ResourcesUtils.getString(R.string.service_flow_month), data.get(index - 1).getMonth()));
                ivBillTimeRight.setClickable(true);
                ivBillTimeRight.setSelected(true);
                break;
            case R.id.iv_bill_time_right:
                index--;
                month = data.get(index).getMonth();
                setMoneyAndList();
                if (index == 0) {
                    ivBillTimeRight.setClickable(false);
                    ivBillTimeRight.setSelected(false);
                    tvBillTimeRight.setText("");
                } else {
                    tvBillTimeRight.setText(String.format(ResourcesUtils.getString(R.string.service_flow_month), data.get(index - 1).getMonth()));
                }
                tvBillTimeLeft.setText(String.format(ResourcesUtils.getString(R.string.service_flow_month), data.get(index + 1).getMonth()));
                ivBillTimeLeft.setClickable(true);
                ivBillTimeLeft.setSelected(true);
                break;
        }
    }
}
